/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utilities;

import GUI.MainFunction;
import connection.ManLabConnection;
import entities.BillingDetails;
import entities.BillingTestDetail;
import entities.PatientDetails;
import entities.Test;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Dell
 */
public class BillManage {

    private ArrayList<BillingDetails> billList;
    private ArrayList<Test> testList;
    private ArrayList<BillingTestDetail> billTestList;
    private ArrayList<PatientDetails> patientList = new ArrayList<PatientDetails>();
    private SimpleDateFormatter sdf = new SimpleDateFormatter();
    private ManLabConnection cnn=new ManLabConnection();
    private ResultSet rs;
    private String qry, qry1, qry2;
    private MainFunction mf;

    public BillManage(ArrayList<BillingDetails> billList) {
        this.billList = new ArrayList<BillingDetails>();
        this.billList = billList;
    }

    public BillManage() {

        

    }

    public BillManage(ArrayList<BillingDetails> billList, ArrayList<Test> testList, MainFunction mf) {
        this.billList=new ArrayList<BillingDetails>();
        this.billList = billList;
        this.testList = testList;
     
        cnn = new ManLabConnection();
        this.mf = mf;
    }

   

    public boolean createNewBill(BillingDetails bd) {
   // billList=new ArrayList<BillingDetails>();
   
//        String a=bd.getPatientDetails().getpFName();
//        JOptionPane.showMessageDialog(mf, a);

        cnn.createConnection();
        //  JOptionPane.showMessageDialog(mf, bd.getbDate());

        qry1 = "insert into Billing(bDate,bAmount,pID) values('" + sdf.convertDateToString(bd.getbDate()) + "'," + bd.getbAmount() + "," + bd.getPatientDetails().getpID() + ")";

        boolean pass1 = cnn.executeQuery(qry1);


        qry = "select top 1 * from Billing order by bID desc";
        rs = cnn.createResultSet(qry);
        int bid = 0;
        boolean exc2 = false;
        try {
            while (rs.next()) {
                bid = rs.getInt("bID");
                bd.setbID(bid);

            }
        } catch (SQLException ex) {
            Logger.getLogger(BillManage.class.getName()).log(Level.SEVERE, null, ex);
        }

    //   JOptionPane.showMessageDialog(mf, bid);

        int i = bd.getBillingTestDetail().size();


        boolean pass = false;
        if (i > 0) {

            for (int j = 0; j < i; j++) {
                qry2 = "insert into BillDetail values(" + bid + "," + bd.getBillingTestDetail().get(j).getTest().getTestID() + "," + bd.getBillingTestDetail().get(j).getTestCost() + ")";
                pass = cnn.executeQuery(qry2);
            }

        }

        cnn.closeConnection();
        if (pass == true && pass1 == true) {
            this.billList.add(bd);
            mf.reviewModel();
            JOptionPane.showMessageDialog(mf, "Register Succesfully!!");
            return true;

        } else {
            JOptionPane.showMessageDialog(mf, "ERROR!!!!");
            return false;
        }
    }

    public boolean updateBill(BillingDetails bd) {
        cnn.createConnection();


        qry = "update Billing set bDate='"+sdf.convertDateToString(bd.getbDate())+"',bAmount=" + bd.getbAmount() + ",pID=" + bd.getPatientDetails().getpID() + " where bID=" + bd.getbID();

        boolean pass1 = cnn.executeQuery(qry);

        qry1 = "delete from BillDetail where bID=" + bd.getbID();
        boolean pass2 = cnn.executeQuery(qry1);

        int i = bd.getBillingTestDetail().size();


        int bid = 0;
        bid = bd.getbID();

        boolean pass = false;
        if (i > 0) {

            for (int j = 0; j < i; j++) {
                qry2 = "insert into BillDetail values(" + bid + "," + bd.getBillingTestDetail().get(j).getTest().getTestID() + "," + bd.getBillingTestDetail().get(j).getTestCost() + ")";
                pass = cnn.executeQuery(qry2);
            }

        }

        cnn.closeConnection();
        if (pass1 == true) {

            mf.reviewModel();
            JOptionPane.showMessageDialog(mf, "Update Succesfully!!");
            return true;
        } else {
            JOptionPane.showMessageDialog(mf, "ERROR!!!!");
            return false;
        }
    }
}
